using UnityEngine;
using System.Collections;
using System.IO;

public class inGameMenuScript : MonoBehaviour {
    mapGenerationScript generator;
    MidiSequence[] midiSequences;
    int currentSong = 0;

    public GUISkin skin;
    public float relMenuWidth = 0.8f;
    public float relMenuHeight = 0.8f;
    public float spaceSize = 15f;
    string title = "Menu";

    public int currentMenu = 1;
    const int HIDE_MENU = 0;
    const int ROOT_MENU = 1;
    const int DIFFICULTY_MENU = 2;
    const int SONGS_MENU = 3;
    const int TRACKS_MENU = 4;

	// Use this for initialization
	void Start () {
        generator = GameObject.Find("world").GetComponent<mapGenerationScript>();
        midiSequences = generator.parseMidiFolder();


    }

    void OnGUI()
    {
        if (currentMenu > 0)
        {
            float x = Screen.width / 2 - Screen.width * relMenuWidth / 2;
            float y = Screen.height / 2 - Screen.height * relMenuHeight / 2;
            float width = Screen.width * relMenuWidth;
            float height = Screen.height * relMenuHeight;
            GUI.skin = skin;
            GUILayout.BeginArea(new Rect(x, y, width, height));
            GUILayout.BeginVertical();
            GUILayout.Box(title);

            switch (currentMenu)
            {
                case ROOT_MENU:
                    currentMenu = showRoot();
                    break;

                case DIFFICULTY_MENU:

                    break;

                case SONGS_MENU:
                    currentMenu = showSongs();
                    break;

                case TRACKS_MENU:
                    currentMenu = showTracks();
                    break;

            }
            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
    }

    int showRoot()
    {
        int next = ROOT_MENU;
        if (GUILayout.Button("Choix du niveau"))
        {
            title = "Choix du niveau";
            next = SONGS_MENU;
        }

        GUILayout.Space(spaceSize);
        if (GUILayout.Button("Quitter"))
            Application.Quit();

        return next;
    }

    int showSongs()
    {
        int next = SONGS_MENU;
        for(int i = 0; i < midiSequences.Length; i++)
        {
            MidiSequence midiSequence = midiSequences[i];
            if (GUILayout.Button(midiSequence.name))
            {
                currentSong = i;
                title = midiSequence.name;
                next = TRACKS_MENU;

            }
        }
        GUILayout.Space(spaceSize);
        if (GUILayout.Button("Retour"))
        {
            title = "Menu";
            next = ROOT_MENU;
        }
        return next;
    }

    int showTracks()
    {
        int next = TRACKS_MENU;
        MidiSequence midiSequence = midiSequences[currentSong];
        ArrayList tracks = (ArrayList)midiSequence.tracks;
        int nbTracks = tracks.Count;
        for (int i = 0; i < nbTracks; i++)
        {
            string trackName = "Piste " + i;
            Hashtable track = (Hashtable)tracks[i];
            Hashtable infos = (Hashtable)track["infos"];
            if (infos.ContainsKey("trackname"))
                trackName = (string)infos["trackname"];
            if (GUILayout.Button(trackName))
            {
                GeneralParameters.numTrack = i;
                generator.generateAttracts(midiSequence);
                GameObject.Find("world").GetComponent<initialisationScript>().prepareToTakeOff();
                next = HIDE_MENU;

            }
        }
        if(!midiSequence.multinotes)
            GeneralParameters.onPartition = GUILayout.Toggle(GeneralParameters.onPartition, "Mode partition");

        GUILayout.Space(spaceSize);
        if (GUILayout.Button("Retour"))
        {
            title = "Choix du niveau";
            next = SONGS_MENU;
        }
        return next;
    }


}
